/*     */ package com.newcrm.db;
/*     */ 
/*     */ import com.newcrm.exception.DBException;
/*     */ import java.sql.Connection;
/*     */ import java.sql.SQLException;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.apache.commons.dbutils.DbUtils;
/*     */ import org.apache.commons.dbutils.GenKeyQueryRunner;
/*     */ import org.apache.commons.dbutils.MyMapListHandler;
/*     */ import org.apache.commons.dbutils.QueryRunner;
/*     */ import org.apache.commons.dbutils.handlers.ArrayHandler;
/*     */ import org.apache.commons.dbutils.handlers.ScalarHandler;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class GroupManager
/*     */ {
/*  21 */   private static Logger logger = Logger.getLogger(GroupManager.class
/*  22 */     .getName());
/*     */ 
/*     */   public List<Map<String, Object>> getGroupInfo(int groupId)
/*     */     throws DBException, SQLException
/*     */   {
/*  34 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  35 */     if (conn == null) {
/*  36 */       throw new DBException();
/*     */     }
/*  38 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/*  40 */       return (List)runner.query(conn, "SELECT GroupId,GroupName,ParentGroupId FROM T_Company_Group_Info WHERE GroupId = ? ", 
/*  41 */         new MyMapListHandler(), new Object[] { Integer.valueOf(groupId) });
/*     */     } finally {
/*     */       try {
/*  44 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/*  46 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public List<Map<String, Object>> list(int companyId)
/*     */     throws DBException, SQLException
/*     */   {
/*  61 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  62 */     if (conn == null) {
/*  63 */       throw new DBException();
/*     */     }
/*  65 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/*  67 */       return (List)runner.query(conn, "SELECT GroupId,GroupName,ParentGroupId FROM T_Company_Group_Info WHERE CompanyId = ? ORDER BY ParentGroupId, GroupId ASC", 
/*  68 */         new MyMapListHandler(), new Object[] { Integer.valueOf(companyId) });
/*     */     } finally {
/*     */       try {
/*  71 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/*  73 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public int add(String name, int parentGroupId, int companyId, String userId)
/*     */   {
/*  87 */     if ((name == null) || (name.length() == 0)) {
/*  88 */       return -1;
/*     */     }
/*  90 */     if (companyId <= 0) {
/*  91 */       return -1;
/*     */     }
/*  93 */     if (parentGroupId < 0) {
/*  94 */       return -1;
/*     */     }
/*     */ 
/*  97 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  98 */     if (conn == null) {
/*  99 */       return -1;
/*     */     }
/*     */ 
/*     */     try
/*     */     {
/* 104 */       QueryRunner runner2 = new QueryRunner();
/*     */       try {
/* 106 */         Object[] result = (Object[])runner2.query(conn, "SELECT GroupId FROM T_Company_Group_Info WHERE CompanyId = ? AND ParentGroupId = ? AND GroupName = ?", 
/* 107 */           new ArrayHandler(), new Object[] { Integer.valueOf(companyId), Integer.valueOf(parentGroupId), name });
/* 108 */         if (result != null)
/* 109 */           return -1;
/*     */       }
/*     */       catch (SQLException e1) {
/* 112 */         logger.warn("check group name failed", e1);
/* 113 */         return -1;
/*     */       }
/*     */ 
/* 116 */       GenKeyQueryRunner runner = new GenKeyQueryRunner(
/* 117 */         new ScalarHandler(), new String[] { "GroupId" });
/*     */       try {
/* 119 */         runner.update(conn, "INSERT INTO T_Company_Group_Info (CompanyId, GroupName, ParentGroupId, CreateDate, UpdateDate, CreateUserId, UpdateUserId) values (?, ?, ?, NOW(), NOW(),?, ?) ", new Object[] { Integer.valueOf(companyId), name, 
/* 120 */           Integer.valueOf(parentGroupId), userId, userId });
/* 121 */         return ((Long)runner.getGeneratedKeys()).intValue();
/*     */       } catch (SQLException e) {
/* 123 */         logger.warn("create group failed", e);
/*     */       }
/*     */     } finally {
/*     */       try {
/* 127 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 129 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */     try
/*     */     {
/* 127 */       DbUtils.close(conn);
/*     */     } catch (SQLException e) {
/* 129 */       logger.warn("close conn failed", e);
/*     */     }
/*     */ 
/* 132 */     return -1;
/*     */   }
/*     */ 
/*     */   public int update(int groupId, int companyId, String name, String userId)
/*     */   {
/* 145 */     if (groupId < 0) {
/* 146 */       return 0;
/*     */     }
/*     */ 
/* 149 */     if ((name == null) || (name.length() == 0)) {
/* 150 */       return 0;
/*     */     }
/*     */ 
/* 153 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 154 */     if (conn == null) {
/* 155 */       return -1;
/*     */     }
/*     */ 
/*     */     try
/*     */     {
/* 160 */       QueryRunner runner = new QueryRunner();
/*     */       try {
/* 162 */         Object[] result = (Object[])runner.query(conn, "SELECT GroupId FROM T_Company_Group_Info WHERE CompanyId = ? AND GroupName = ? AND GroupId <> ?", 
/* 163 */           new ArrayHandler(), new Object[] { Integer.valueOf(companyId), name, Integer.valueOf(groupId) });
/* 164 */         if (result != null)
/* 165 */           return 0;
/*     */       }
/*     */       catch (SQLException e1) {
/* 168 */         logger.warn("check group name failed", e1);
/* 169 */         return 0;
/*     */       }
/*     */       try
/*     */       {
/* 173 */         return runner.update(conn, "UPDATE T_Company_Group_Info SET GroupName = ?, UpdateDate = NOW(), UpdateUserId = ? WHERE GroupId = ?", new Object[] { name, 
/* 174 */           userId, Integer.valueOf(groupId) });
/*     */       } catch (SQLException e) {
/* 176 */         logger.warn("update group name failed", e);
/*     */       }
/*     */     } finally {
/*     */       try {
/* 180 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 182 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */     try
/*     */     {
/* 180 */       DbUtils.close(conn);
/*     */     } catch (SQLException e) {
/* 182 */       logger.warn("close conn failed", e);
/*     */     }
/*     */ 
/* 185 */     return 0;
/*     */   }
/*     */ 
/*     */   public int delete(int groupId)
/*     */   {
/* 195 */     if (groupId < 0) {
/* 196 */       return 0;
/*     */     }
/*     */ 
/* 199 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 200 */     if (conn == null) {
/* 201 */       return -1;
/*     */     }
/*     */     try
/*     */     {
/* 205 */       delete(conn, groupId);
/* 206 */       return 1;
/*     */     } catch (SQLException e) {
/* 208 */       logger.warn("delete group failed", e);
/*     */     } finally {
/*     */       try {
/* 211 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 213 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/* 216 */     return 0;
/*     */   }
/*     */ 
/*     */   private void delete(Connection conn, int groupId)
/*     */     throws SQLException
/*     */ {
/* 227 */
    QueryRunner runner = new QueryRunner();
/*     */ 
/* 230 */
    runner.update(conn, "DELETE FROM T_Company_Group_Info WHERE GroupId = ?", Integer.valueOf(groupId));
/*     */ 
/* 233 */
    runner.update(conn, "UPDATE T_User_Info SET GroupId = 0, IsGroupAdmin = 0 WHERE GroupId = ?", Integer.valueOf(groupId));
/*     */ 
/* 236 */
    List children = (List) runner.query(conn,
/* 237 */       "SELECT GroupId FROM T_Company_Group_Info WHERE ParentGroupId = ? ", new MyMapListHandler(), new Object[]{Integer.valueOf(groupId)});
/* 238 */
    if ((children == null) || (children.size() == 0)) {
/* 239 */
        return;
/*     */
    }
/*     */
    for (Object itemO : children) {
        Map item = (Map) itemO;
/* 243 */
        delete(conn, ((Integer) item.get("GroupId")).intValue());
/*     */
    }
}
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.db.GroupManager
 * JD-Core Version:    0.6.1
 */